package com.leo.model;

import com.leo.util.DateUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;

/**
 * @ClassName: UserInfo
 * @Description: 用户信息表
 * @Author: leo825
 * @Date: 2020-01-02 11:10
 * @Version: 1.0
 */
public class UserInfo implements BeanFactoryAware,
        BeanNameAware,
        BeanClassLoaderAware,
        ResourceLoaderAware,
        MessageSourceAware,
        EnvironmentAware,
        ApplicationContextAware,
        InitializingBean,
        DisposableBean {
    //用户id
    Integer id;
    //用户名称
    String name;
    //用户性别
    String gender;
    //用户年龄
    String age;
    //备注
    String remarks;

    public UserInfo() {
        System.out.println(DateUtil.getTimeNow() + "调用UserInfo构造方法");
    }

    public void myInit() {
        System.out.println(DateUtil.getTimeNow() + "调用UserInfo的myInit()方法");
    }

    public void myDestroy() {
        System.out.println(DateUtil.getTimeNow() + "调用UserInfo的myDestroy()方法");
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        System.out.println(DateUtil.getTimeNow() + "调用UserInfo赋值属性的setId()");
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println(DateUtil.getTimeNow() + "调用UserInfo赋值属性的setName()");
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        System.out.println(DateUtil.getTimeNow() + "调用UserInfo赋值属性的setAge()");
        this.age = age;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    @Override
    public String toString() {
        System.out.println(DateUtil.getTimeNow() + "调用UserInfo赋值属性的toString()");
        return DateUtil.getTimeNow() + "UserInfo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age='" + age + '\'' +
                ", remarks='" + remarks + '\'' +
                '}';
    }


    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println(DateUtil.getTimeNow() + "调用BeanClassLoaderAware的setBeanClassLoader()");
    }


    @Override
    public void setBeanName(String s) {
        System.out.println(DateUtil.getTimeNow() + "调用BeanNameAware的setBeanName()");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println(DateUtil.getTimeNow() + "调用ApplicationContextAware的setApplicationContext()");
        //模拟获取系统运行参数
        String sysProp = applicationContext.getEnvironment().getProperty("System.id");
        System.out.println(DateUtil.getTimeNow() + "获取系统参数System.id=" + sysProp);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println(DateUtil.getTimeNow() + "调用BeanFactoryAware的setBeanFactory()");
    }

    @Override
    public void setEnvironment(Environment environment) {
        System.out.println(DateUtil.getTimeNow() + "调用EnvironmentAware的setEnvironment()");
        System.out.println(DateUtil.getTimeNow() + environment.getProperty("user.dir"));

        //设置系统运行参数
        String sysProp = "springtest";
        System.out.println(DateUtil.getTimeNow() + "设置系统参数System.id=" + sysProp);
        System.setProperty("System.id", sysProp);

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(DateUtil.getTimeNow() + "调用InitializingBean的afterPropertiesSet()");

    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        System.out.println(DateUtil.getTimeNow() + "调用MessageSourceAware的setMessageSource()");
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        System.out.println(DateUtil.getTimeNow() + "调用BeanClassLoaderAware的setBeanClassLoader()");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println(DateUtil.getTimeNow() + "调用DisposableBean赋值属性的destroy()");
    }

}
